package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name", userName);
        qw.eq("u_pwd", pwd);
        UserBean user = userMapper.selectOne(qw);
        return user;
    }

    @Override
    public boolean addUser(UserBean user) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name", user.getName());
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean == null) {
            userMapper.insert(user);
            return true;
        }
        return false;
    }

    @Override
    public boolean checkName(String userName) {

        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name", userName);
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean == null) {
            return true;
        }
        return false;
    }

    @Override
    public List<UserBean> findAll() {

        return userMapper.selectList(null);
    }

    @Override
    public boolean delUser(int id, UserBean loginUser) {
        if (id == loginUser.getId()) {
            return false;
        } else {
            userMapper.deleteById(id);
            return true;
        }
    }

    @Override
    public UserBean findById(int id) {
        return userMapper.selectById(id);
    }

    @Override
    public void updateUserLevel(int id, String level) {
        UserBean userBean = new UserBean();
        userBean.setLevel(level);
        userBean.setId(id);
        userMapper.updateById(userBean);

    }

    @Override
    public void updateUserPwd(int id, String pwd) {
        UserBean userBean = new UserBean();
        userBean.setPwd(pwd);
        userBean.setId(id);
        userMapper.updateById(userBean);
    }

}
